//
//  LZRushCouponDetailViewController.m
//  LzzizDemo
//
//  Created by xbnzd on 16/11/7.
//  Copyright © 2016年 xbnzd. All rights reserved.
//

#import "LZGrabCouponDetailViewController.h"
#import "LZGrabCouponDetailMealsTimeCell.h"
#import "LZGrabCouponDetailTitleCell.h"
#import "LZGrabCouponDetailOrderRulesCell.h"
#import "LZGrabCouponDetailRulesOfUseCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "LZCustomPlayerView.h"
#import "Masonry.h"
#import "UIView+Rotation.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZCouponPresenter.h"
#import "LZPlayVideoManager.h"
#import "LZVideoPublisherCell.h"
#import "LZDateFormatterManager.h"
#import "LZCouponListCouponDetailModel.h"
#import "LZCouponModel.h"
#import "AppDelegate.h"
#import "UIImageView+WebCache.h"

static NSString *kTitleCell = @"titleCell";
static NSString *kRulesOfUseCell = @"rulesOfUseCell";
static NSString *kMealsTimeCell  = @"mealsTimeCell";
static NSString *kPubliserCell = @"publisherCell";

@interface LZGrabCouponDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) LZCustomPlayerView *playerView;
@property (nonatomic, strong) UIView *videoBgView;
@property (nonatomic, strong) UIImageView *videoCoverImage;
@property (strong, nonatomic) UIButton *nav_left_back;
@property (assign, nonatomic) CGFloat navbar_change_y;
@property (assign, nonatomic) CGFloat lzAlpha;
@property (strong, nonatomic) LZCouponPresenter *couponPresenter;
@property (strong, nonatomic) LZCouponListCouponDetailModel *dataModel;
@end

@implementation LZGrabCouponDetailViewController
- (id)init
{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.umLogPageViewName = @"订单-优惠券详情";
    
    _couponPresenter = [[LZCouponPresenter alloc] init];
    [self initUI];
    [self configNavigationBar];
    [self registerCell];
    [self getData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)dealloc
{
    [self releasePlayViewRAM];
}

- (BOOL)fd_prefersNavigationBarHidden {
    return YES;
}


#pragma mark -
#pragma mark network method
- (void)getData
{
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:NO];
    WS(wself);
    [self.couponPresenter getCouponDetailWithCouponID:self.couponId ResultSuccess:^(LZCouponListCouponDetailModel *couponModel) {
        
        wself.dataModel = couponModel;
    
        [wself.videoCoverImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,couponModel.videoPhoto]] placeholderImage:[UIImage imageNamed:@"videoCoverImg"]];
        
        [wself.tableView reloadData];
        
        [wself playVideo];
        
        [wself stopLoadingViewForView:wself.view];
        
    } Failure:^(NSString *fail) {
        [wself stopLoadingViewForView:wself.view];
        DLog(@"%@",fail);
    }];
}

-(void)navbarBackButtonClickAction:(UIButton*)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UI
- (void)initUI
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.navbar_change_y = SCREEN_WIDTH/375.0*(500-40)/2.0-64;
}

- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:@"LZGrabCouponDetailTitleCell" bundle:nil] forCellReuseIdentifier:kTitleCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"LZGrabCouponDetailRulesOfUseCell" bundle:nil] forCellReuseIdentifier:kRulesOfUseCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"LZGrabCouponDetailMealsTimeCell" bundle:nil] forCellReuseIdentifier:kMealsTimeCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"LZVideoPublisherCell" bundle:nil] forCellReuseIdentifier:kPubliserCell];
    
    [self.videoBgView addSubview:self.videoCoverImage];
    self.tableView.tableHeaderView = self.videoBgView;
}

#pragma mark -
#pragma mark private method
-(void)configNavigationBar{
    
    self.nav_left_back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nav_left_back.frame = CGRectMake(0, 0, 60, 40);
    [self.nav_left_back addTarget:self action:@selector(navbarBackButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *image_back = [UIImage imageNamed:@"navigationbar_back_circle"];
    [self.nav_left_back setImage:image_back forState:UIControlStateNormal];
    self.nav_left_back.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.nav_left_back.bounds)-image_back.size.width);
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        [self.nav_left_back setLanguageRotaion];
    }
    
    
    [self hiddenBarNavigationItemSetting:@[self.nav_left_back] type:NAVIGATIONITEMSETTING_LEFT];
    [self lz_setCustomBarAlpha:self.lzAlpha];
    
}

- (void)playVideo
{
    [[LZPlayVideoManager shareManager] playVideoWithBgView:self.videoBgView playerView:self.playerView videoURL:self.dataModel.videoUrl videoID:nil playCountAddBlock:^(NSInteger totleCount) {
        
    }];
}

/**
 离开页面,释放播放器内存
 */
- (void)releasePlayViewRAM
{
    [self.playerView stopAction];
    _playerView = nil;
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

#pragma mark -
#pragma mark tableview  delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(wSelf);
    switch (indexPath.row) {
        case 0:
            return [tableView fd_heightForCellWithIdentifier:kTitleCell configuration:^(id cell) {
                LZGrabCouponDetailTitleCell *cellTemp = (LZGrabCouponDetailTitleCell *)cell;
                [cellTemp loadTitle:wSelf.dataModel.coupons.couponsName];
            }];
            break;
        case 1:
            return [tableView fd_heightForCellWithIdentifier:kRulesOfUseCell configuration:^(id cell) {
                LZGrabCouponDetailRulesOfUseCell *cellTemp = (LZGrabCouponDetailRulesOfUseCell *)cell;
                [cellTemp loadTitle:LZLocalizedString(@"group_terms_of_use", nil) contentStr:wSelf.dataModel.coupons.couponsUseConditions guizeContent:wSelf.dataModel.coupons.couponsRemindRule];
            }];
            break;
        case 2:
            return [tableView fd_heightForCellWithIdentifier:kMealsTimeCell configuration:^(id cell) {
                LZGrabCouponDetailMealsTimeCell *cellTemp = (LZGrabCouponDetailMealsTimeCell *)cell;
                NSString *everyDayValid = [wSelf.dataModel.coupons getCouponEverydayValid];

                 [cellTemp loadCouponValidityPeriod:[NSString stringWithFormat:@"%@ - %@",[[LZDateFormatterManager shareInstance] stringFromDoubleTimestampHavePointyyyyMMdd:wSelf.dataModel.coupons.couponsStartTime],[[LZDateFormatterManager shareInstance] stringFromDoubleTimestampHavePointyyyyMMdd:wSelf.dataModel.coupons.couponsEndTime]] ValidExcept:wSelf.dataModel.coupons.couponsExceptTime EverydayValid:everyDayValid otherContent:wSelf.dataModel.coupons.couponsRemindAppoint];
                
            }];
            break;
        case 3:
            return [tableView fd_heightForCellWithIdentifier:kPubliserCell configuration:^(id cell) {
                LZVideoPublisherCell *cellTemp = (LZVideoPublisherCell *)cell;
                [cellTemp loadPublisherName:wSelf.dataModel.restaurantName];
            }];
            break;
            
        default:
            break;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:{
            LZGrabCouponDetailTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:kTitleCell];
            [cell loadTitle:self.dataModel.coupons.couponsName];
            
            return cell;
        }

            break;
        case 1:{
            LZGrabCouponDetailRulesOfUseCell *cell = [tableView dequeueReusableCellWithIdentifier:kRulesOfUseCell];
            [cell loadTitle:LZLocalizedString(@"group_terms_of_use", nil) contentStr:self.dataModel.coupons.couponsUseConditions guizeContent:self.dataModel.coupons.couponsRemindRule];
            
            return cell;
        }
            
            break;
        case 2:{
            LZGrabCouponDetailMealsTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:kMealsTimeCell];
            NSString *everyDayValid = [self.dataModel.coupons getCouponEverydayValid];

            [cell loadCouponValidityPeriod:[NSString stringWithFormat:@"%@ - %@",[[LZDateFormatterManager shareInstance] stringFromDoubleTimestampHavePointyyyyMMdd:self.dataModel.coupons.couponsStartTime],[[LZDateFormatterManager shareInstance] stringFromDoubleTimestampHavePointyyyyMMdd:self.dataModel.coupons.couponsEndTime]] ValidExcept:self.dataModel.coupons.couponsExceptTime EverydayValid:everyDayValid otherContent:self.dataModel.coupons.couponsRemindAppoint];

            
            return cell;
        }
            
            break;
        case 3:{
            LZVideoPublisherCell *cell = [tableView dequeueReusableCellWithIdentifier:kPubliserCell];
            [cell loadPublisherName:self.dataModel.restaurantName];
            
            return cell;
        }

            break;
            
        default:
            break;
    }

    return nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY <= self.navbar_change_y) {
        CGFloat alpha = MIN(1, offsetY/self.navbar_change_y);
        
        self.lzAlpha = alpha;
        
        CGFloat navbar_item_alpha = fabs((alpha-0.5)/(0.5));
        self.nav_left_back.alpha = navbar_item_alpha;
        if (alpha <= 0.5) {
            [self.nav_left_back setImage:[UIImage imageNamed:@"navigationbar_back_circle"] forState:UIControlStateNormal];
        }
        else {
            [self.nav_left_back setImage:[UIImage imageNamed:@"lz_back_en"] forState:UIControlStateNormal];
        }
        
    } else {
        self.lzAlpha = 1;
    }
    [self lz_setCustomBarAlpha:self.lzAlpha];

}

#pragma mark -
#pragma mark 空间初始化
- (UIView *)videoBgView
{
    if (!_videoBgView) {
        _videoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH)];
        _videoBgView.backgroundColor = [UIColor blackColor];
    }
    
    return _videoBgView;
}

- (UIImageView *)videoCoverImage
{
    if (!_videoCoverImage) {
        _videoCoverImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH)];
        _videoCoverImage.backgroundColor = [UIColor blackColor];
    }
    
    return _videoCoverImage;
}

- (LZCustomPlayerView *)playerView
{
    if (!_playerView) {
        _playerView = [[LZCustomPlayerView alloc] init];
        _playerView.playCircle = NO;
    }
    
    return _playerView;
}

@end
